/*   1:    */ package nl.siegmann.epublib.viewer;
/*   2:    */ 
/*   3:    */ import java.awt.BorderLayout;
/*   4:    */ import java.awt.Dimension;
/*   5:    */ import java.awt.Font;
/*   6:    */ import java.awt.event.ActionEvent;
/*   7:    */ import java.awt.event.ActionListener;
/*   8:    */ import java.awt.event.KeyEvent;
/*   9:    */ import java.awt.event.KeyListener;
/*  10:    */ import java.util.List;
/*  11:    */ import javax.swing.JButton;
/*  12:    */ import javax.swing.JPanel;
/*  13:    */ import javax.swing.JTextField;
/*  14:    */ import javax.swing.JToolBar;
/*  15:    */ import nl.siegmann.epublib.browsersupport.NavigationEvent;
/*  16:    */ import nl.siegmann.epublib.browsersupport.NavigationEventListener;
/*  17:    */ import nl.siegmann.epublib.browsersupport.NavigationHistory;
/*  18:    */ import nl.siegmann.epublib.browsersupport.Navigator;
/*  19:    */ import nl.siegmann.epublib.domain.Book;
/*  20:    */ import nl.siegmann.epublib.search.SearchIndex;
/*  21:    */ import nl.siegmann.epublib.search.SearchResult;
/*  22:    */ import nl.siegmann.epublib.search.SearchResults;
/*  23:    */ import nl.siegmann.epublib.util.ToolsResourceUtil;
/*  24:    */ 
/*  25:    */ public class NavigationBar
/*  26:    */   extends JToolBar
/*  27:    */   implements NavigationEventListener
/*  28:    */ {
/*  29:    */   private static final long serialVersionUID = 1166410773448311544L;
/*  30:    */   private JTextField titleField;
/*  31:    */   private JTextField searchField;
/*  32:    */   private final NavigationHistory navigationHistory;
/*  33:    */   private Navigator navigator;
/*  34: 42 */   private SearchIndex searchIndex = new SearchIndex();
/*  35: 43 */   private String previousSearchTerm = null;
/*  36: 44 */   private int searchResultIndex = -1;
/*  37:    */   private SearchResults searchResults;
/*  38:    */   
/*  39:    */   public NavigationBar(Navigator navigator)
/*  40:    */   {
/*  41: 48 */     this.navigationHistory = new NavigationHistory(navigator);
/*  42: 49 */     this.navigator = navigator;
/*  43: 50 */     navigator.addNavigationEventListener(this);
/*  44: 51 */     addHistoryButtons();
/*  45: 52 */     this.titleField = ((JTextField)add(new JTextField()));
/*  46: 53 */     addSearchButtons();
/*  47: 54 */     initBook(navigator.getBook());
/*  48:    */   }
/*  49:    */   
/*  50:    */   private void initBook(Book book)
/*  51:    */   {
/*  52: 58 */     if (book == null) {
/*  53: 59 */       return;
/*  54:    */     }
/*  55: 61 */     this.searchIndex.initBook(book);
/*  56:    */   }
/*  57:    */   
/*  58:    */   private void addHistoryButtons()
/*  59:    */   {
/*  60: 65 */     Font historyButtonFont = new Font("SansSerif", 1, 24);
/*  61: 66 */     JButton previousButton = ViewerUtil.createButton("history-previous", "<=");
/*  62: 67 */     previousButton.setFont(historyButtonFont);
/*  63:    */     
/*  64:    */ 
/*  65: 70 */     previousButton.addActionListener(new ActionListener()
/*  66:    */     {
/*  67:    */       public void actionPerformed(ActionEvent e)
/*  68:    */       {
/*  69: 74 */         NavigationBar.this.navigationHistory.move(-1);
/*  70:    */       }
/*  71: 77 */     });
/*  72: 78 */     add(previousButton);
/*  73:    */     
/*  74: 80 */     JButton nextButton = ViewerUtil.createButton("history-next", "=>");
/*  75: 81 */     nextButton.setFont(historyButtonFont);
/*  76: 82 */     nextButton.addActionListener(new ActionListener()
/*  77:    */     {
/*  78:    */       public void actionPerformed(ActionEvent e)
/*  79:    */       {
/*  80: 86 */         NavigationBar.this.navigationHistory.move(1);
/*  81:    */       }
/*  82: 88 */     });
/*  83: 89 */     add(nextButton);
/*  84:    */   }
/*  85:    */   
/*  86:    */   private void doSearch(int move)
/*  87:    */   {
/*  88: 93 */     String searchTerm = this.searchField.getText();
/*  89: 94 */     if (searchTerm.equals(this.previousSearchTerm))
/*  90:    */     {
/*  91: 95 */       this.searchResultIndex += move;
/*  92:    */     }
/*  93:    */     else
/*  94:    */     {
/*  95: 97 */       this.searchResults = this.searchIndex.doSearch(searchTerm);
/*  96: 98 */       this.previousSearchTerm = searchTerm;
/*  97: 99 */       this.searchResultIndex = 0;
/*  98:    */     }
/*  99:101 */     if (this.searchResultIndex < 0) {
/* 100:102 */       this.searchResultIndex = (this.searchResults.size() - 1);
/* 101:103 */     } else if (this.searchResultIndex >= this.searchResults.size()) {
/* 102:104 */       this.searchResultIndex = 0;
/* 103:    */     }
/* 104:106 */     if (!this.searchResults.isEmpty())
/* 105:    */     {
/* 106:107 */       SearchResult searchResult = (SearchResult)this.searchResults.getHits().get(this.searchResultIndex);
/* 107:108 */       this.navigator.gotoResource(searchResult.getResource(), searchResult.getPagePos(), this);
/* 108:    */     }
/* 109:    */   }
/* 110:    */   
/* 111:    */   private void addSearchButtons()
/* 112:    */   {
/* 113:114 */     JPanel searchForm = new JPanel(new BorderLayout());
/* 114:115 */     searchForm.setPreferredSize(new Dimension(200, 28));
/* 115:116 */     Font historyButtonFont = new Font("SansSerif", 1, 20);
/* 116:117 */     JButton previousButton = ViewerUtil.createButton("search-previous", "<");
/* 117:118 */     previousButton.setFont(historyButtonFont);
/* 118:    */     
/* 119:    */ 
/* 120:121 */     previousButton.addActionListener(new ActionListener()
/* 121:    */     {
/* 122:    */       public void actionPerformed(ActionEvent e)
/* 123:    */       {
/* 124:125 */         NavigationBar.this.doSearch(-1);
/* 125:    */       }
/* 126:128 */     });
/* 127:129 */     searchForm.add(previousButton, "West");
/* 128:    */     
/* 129:131 */     this.searchField = new JTextField();
/* 130:    */     
/* 131:    */ 
/* 132:    */ 
/* 133:135 */     this.searchField.setMinimumSize(new Dimension(100, 20));
/* 134:136 */     this.searchField.addKeyListener(new KeyListener()
/* 135:    */     {
/* 136:    */       public void keyTyped(KeyEvent keyEvent) {}
/* 137:    */       
/* 138:    */       public void keyPressed(KeyEvent e) {}
/* 139:    */       
/* 140:    */       public void keyReleased(KeyEvent keyEvent)
/* 141:    */       {
/* 142:148 */         if (keyEvent.getKeyCode() == 10) {
/* 143:149 */           NavigationBar.this.doSearch(1);
/* 144:    */         }
/* 145:    */       }
/* 146:153 */     });
/* 147:154 */     searchForm.add(this.searchField, "Center");
/* 148:155 */     JButton nextButton = ViewerUtil.createButton("search-next", ">");
/* 149:156 */     nextButton.setFont(historyButtonFont);
/* 150:157 */     nextButton.addActionListener(new ActionListener()
/* 151:    */     {
/* 152:    */       public void actionPerformed(ActionEvent e)
/* 153:    */       {
/* 154:161 */         NavigationBar.this.doSearch(1);
/* 155:    */       }
/* 156:163 */     });
/* 157:164 */     searchForm.add(nextButton, "East");
/* 158:165 */     add(searchForm);
/* 159:    */   }
/* 160:    */   
/* 161:    */   public void navigationPerformed(NavigationEvent navigationEvent)
/* 162:    */   {
/* 163:170 */     if (navigationEvent.isBookChanged()) {
/* 164:171 */       initBook(navigationEvent.getCurrentBook());
/* 165:    */     }
/* 166:173 */     if (navigationEvent.getCurrentResource() != null)
/* 167:    */     {
/* 168:174 */       String title = ToolsResourceUtil.getTitle(navigationEvent.getCurrentResource());
/* 169:175 */       this.titleField.setText(title);
/* 170:    */     }
/* 171:    */   }
/* 172:    */ }


/* Location:           C:\Users\David\workspace\CosaRaraPorqueSi\main\epublib-tools-3.1.jar
 * Qualified Name:     nl.siegmann.epublib.viewer.NavigationBar
 * JD-Core Version:    0.7.0.1
 */